jQuery(function(){
  
  function hideForm($form){
    $form.removeClass('visible').slideUp(200);
  }
  
  function tryHide($form){
     if($form.has(':input:focus').length==0){
        $form.slideUp();
      } else {
        setTimeout(function(){
          tryHide($form);
        },1000);
      }
  }
  
  jQuery(document).on('init','.loli-update-score',function(){
    var $ligne = jQuery(this);
    $ligne.hoverIntent(function(event){
      var $this = jQuery(this);
      var $form = $this.find('.form-updateScore');
      if(event.type=="mouseenter"){
        var off   = $this.position();
        var $offP = $this.offsetParent();
        var top   = off.top+$offP.scrollTop();
        var left  = off.left+$offP.scrollLeft();
        $form.css({ 
          top : top
         ,left : left
        });      
       $form.slideDown();
        jQuery(document).one('keyup', function(event){  
          if(event.keyCode==27){ 
            hideForm($form);
          }
        });      
      } else {
        tryHide($form);
      }
    })

    $ligne.find('button[type=submit]').button({
      icons: {
        primary: "ui-icon-disk"
      }
      ,text : false
    });    
  });
  
  jQuery(document).on('keydown','.loli-update-score input',function(event){
    var keys = [8,9,13,16,46,47,48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105];
    if(jQuery.inArray(event.keyCode, keys)==-1){
      event.stopPropagation();
      event.cancelBubble = true;
      return false;
    }
    return true;
  });
  
  jQuery(document).on('submit','.form-updateScore form',function (event){

    event.stopPropagation();
      event.cancelBubble = true;
      
      var $form = jQuery(this);

      $form.find('button').button('option','disabled',true);
      // ON choppe les valeurs ici car si on désactive les inputs, on ne serialize rien
      var post = com.lolita.utils.serializeFormObject($form);

      var $container = $form
        .closest('.form-updateScore')
        .addClass('updating');
      $container.find(':input')
        .attr('disabled','disabled')
        ;

      post = jQuery.extend({},{'action' : 'update','what' : 'score'},post);

      jQuery.post(com.lolita.get.ajaxUrl(),post,function(rep){
        if(rep.status){
          $widget = jQuery(rep.success[0]);
          $container.replaceWith($widget);
          $container  = $widget;
          $widget.trigger('init');
          var scoreA  = parseInt($widget.find('input[name=match_score_A]').val());
          var scoreB  = parseInt($widget.find('input[name=match_score_B]').val());
          var $scores = $container.closest('.loli-update-score').find('span.score_A,span.score_B');
          
          
          function applyScore($this,score,diff){
            $this.fadeOut(function(){
                $this.text(score);
                if(diff>0){
                  $this.wrapInner('<strong></strong>');
                } 
                $this.fadeIn();
              });
          }

          $scores.each(function(){
            var $this = jQuery(this);
            if($this.is('.score_A')){
              applyScore($this,scoreA,scoreA-scoreB);
            } else {
              applyScore($this,scoreB,scoreB-scoreA);
            }
          });
          
          var $score = $container.closest('.loli-update-score').find('.score');
          if(scoreA==0 && scoreB==0){
            $score.addClass('hidden');
          } else {
            if($score.hasClass('hidden')){
              $score.removeClass('hidden');
            }
          }  
          
          

        } else {
          eval(rep.eval);
        }
      },'json');
      return false;
   });
   
   jQuery('.loli-update-score').trigger('init');
  
});

